package aar.test.tessocr.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import clx.tessocr.ocreng.R;

/**
 * 已揽件列表
 * Created by sunshine on 2020/10/20
 */
public class OrderTakenFragment extends BaseFragment {
    FragmentManager fm;
    @BindView(R.id.pay)
    Button pay;
    @BindView(R.id.income)
    Button income;
    private OrderTakenNoPayedFragment incomeFragment;
    private OrderTakenHasPayedFragment payFragment;
    private String search = "";
    private String cabinetId = "";
    private boolean isUpdate;

    public static OrderTakenFragment newInstance(String orderType, String search, String cabinetId, boolean isUpdate) {
        OrderTakenFragment fragment = new OrderTakenFragment();
        Bundle bundle = new Bundle();
        bundle.putString("orderType", orderType);
        bundle.putString("search", search);
        bundle.putString("cabinetId", cabinetId);
        bundle.putBoolean("isUpdate", isUpdate);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        //recruitType = getArguments().getInt(Constants.RECRUIT_TYPE);
        search = getArguments().getString("search");
        cabinetId = getArguments().getString("cabinetId");
        isUpdate = getArguments().getBoolean("isUpdate", false);

    }

    private void initview() {
        search = getArguments().getString("search");
        cabinetId = getArguments().getString("cabinetId");
        isUpdate = getArguments().getBoolean("isUpdate", false);
        fm = getFragmentManager();
        setTabSelection(0);
    }

    private void setTabSelection(int index) {
        FragmentTransaction ft = fm.beginTransaction();
        hideFragment(ft);
        switch (index) {
            case 0:

                if (incomeFragment == null) {
                    incomeFragment = new OrderTakenNoPayedFragment().newInstance("3", search, cabinetId);
                    ft.add(R.id.fl, incomeFragment);
                } else {
                    ft.show(incomeFragment);
                    /*if (isUpdate) {
                        incomeFragment = null;
                        incomeFragment = new OrderTakenNoPayedFragment().newInstance("3", search, cabinetId);
                        ft.add(R.id.fl, incomeFragment);
                    } else {
                        ft.show(incomeFragment);
                    }*/

                }

                break;

            case 1:

                if (payFragment == null) {
                    payFragment = new OrderTakenHasPayedFragment().newInstance("4", search, cabinetId);
                    ft.add(R.id.fl, payFragment);
                } else {
                    ft.show(payFragment);
                    /*if (isUpdate) {
                        payFragment = null;
                        payFragment = new OrderTakenHasPayedFragment().newInstance("4", search, cabinetId);
                        ft.add(R.id.fl, payFragment);
                    } else {
                        ft.show(incomeFragment);
                    }*/
                }

                break;
        }
        ft.commit();
    }

    //用于隐藏fragment
    private void hideFragment(FragmentTransaction ft) {
        if (incomeFragment != null) {
            ft.hide(incomeFragment);
        }
        if (payFragment != null) {
            ft.hide(payFragment);
        }
    }

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_order_taken);
        initview();
    }

    @Override
    protected void setUpView(View view) {

    }

    @Override
    protected void setUpComponent() {

    }

    @Override
    protected void setUpData() {

    }

    @OnClick({R.id.pay, R.id.income})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pay:
                pay.setBackgroundResource(R.drawable.select_interest_cancel);
                pay.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                income.setBackgroundResource(R.drawable.back_red);
                income.setTextColor(getActivity().getResources().getColor(R.color.text_gray));
                setTabSelection(0);
                break;
            case R.id.income:
                income.setBackgroundResource(R.drawable.select_interest_cancel);
                income.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                pay.setBackgroundResource(R.drawable.back_red);
                pay.setTextColor(getActivity().getResources().getColor(R.color.text_gray));
                setTabSelection(1);
                break;
        }
    }
}
